package cs342.project1.mathNotebook.xml;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.Collection;


/**
 * [insert description of class]
 * @author Gabrielle
 *
 */
public class CompressionReader extends NotebookReader {
	private static int bufferSize = 8192;

	/**
     * @see cs342.project1.mathNotebook.xml.NotebookReader#NotebookReader()
     */
	public CompressionReader(InputStream inStream){
		super(inStream);
    }//constructor

    /**
     * Decompresses data from passed in InputStream into an OutputStream
     * and then converts OutputStream into an InputStream to return
     *
     * @param instream an InputStream object containing the file
     * @return An InputStream containing decompressed data from file
     */
    public InputStream loadStream(InputStream instream) {
		GZIPInputStream zipin;

		try{
			zipin = new GZIPInputStream(instream);

			byte[] buffer = new byte[bufferSize];
			ByteArrayOutputStream out = new ByteArrayOutputStream();

			//decompress data into ByteArrayOutputStream
			int length;
			while ((length = zipin.read(buffer, 0, bufferSize)) != -1)
				out.write(buffer, 0, length);

			//convert ByteArrayOutputStream back into InputStream
			in = new ByteArrayInputStream(out.toByteArray());

			out.close(  );
			zipin.close(  );
		} catch (IOException e) {
			System.out.println("Couldn't decompress. CompressionReader.loadStream() error: " + e);
		}

		return in;
	}//loadStream()

}//CompressionReader
